package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.model.Store;
import com.lovo.service.StoreService;
import com.lovo.service.impl.StoreServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "StoreServlet",urlPatterns = "/store.do")
public class StoreServlet extends HttpServlet {
    private StoreService ss = new StoreServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String method = request.getParameter("method");
        if (method == null || "".equals(method)) {
            showAll(request, response);
        }else if (method.equalsIgnoreCase("add")) {
            Add(request, response);
            //添加
        }else if(method.equalsIgnoreCase("delete")){
            delete(request,response);
            //删除
        }
        else if(method.equalsIgnoreCase("update")){
            update(request,response);
            //审核
        }
        else if(method.equalsIgnoreCase("found")){
            found(request,response);
            //查看
        }
    }
    private void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = 1;
        Store store = new Store();
        List<Store> list = ss.getAll(store);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        response.getWriter().write(json);

    }
    private void Add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String store_name = request.getParameter("store_name");
        int store_license_number = Integer.parseInt(request.getParameter("store_license_number"));
        String store_license_images = request.getParameter("store_license_images");
        String store_address = request.getParameter("store_address");
        String store_location = request.getParameter("store_location");
        String store_master = request.getParameter("store_master");
        String store_tel = request.getParameter("store_tel");
        String store_special = request.getParameter("store_special");
        String store_commission = request.getParameter("store_commission");
        String store_vip = request.getParameter("store_vip");
        String store_examine = "未审核";

        Store s = new Store();
        s.setStore_name(store_name);
        s.setStore_license_number(store_license_number);
        s.setStore_license_images(store_license_images);
        s.setStore_address(store_address);
        s.setStore_location(store_location);
        s.setStore_master(store_master);
        s.setStore_tel(store_tel);
        s.setStore_special(store_special);
        s.setStore_commission(store_commission);
        s.setStore_vip(store_vip);
        s.setStore_examine(store_examine);

        boolean flag = ss.insertStore(s);
        PrintWriter out = response.getWriter();
        out.write(String.valueOf(flag));
    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int store_id=Integer.parseInt(request.getParameter("store_id"));
        System.out.println("store_id = " + store_id);
        boolean flag=ss.deleteStore(store_id);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(flag);
        System.out.println("json = " + json);
        response.getWriter().write(json);
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int store_id = Integer.parseInt(request.getParameter("store_id"));
        String store_examine = "已审核";
        Store s = new Store();
        s.setStore_examine(store_examine);
        s.setStore_id(store_id);
        boolean flag=ss.updateStore(s);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(flag);
        System.out.println("json = " + json);
        response.getWriter().write(json);

    }
    protected void found(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int store_id = Integer.parseInt(request.getParameter("store_id"));
        Store store = new Store();
        List<Store> list = ss.getStoreById(store_id);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        response.getWriter().write(json);
    }

}